package org.dawning.shop.order.rest;

import org.axonframework.commandhandling.gateway.CommandGateway;
import org.dawning.shop.common.commands.order.CreateOrderCommand;
import org.dawning.shop.common.domain.order.OrderId;
import org.dawning.shop.common.dto.OrderProductDto;
import org.dawning.shop.common.vo.Address;
import org.dawning.shop.common.vo.CategoryAttribute;
import org.dawning.shop.order.application.OrderAppService;
import org.dawning.shop.order.common.dto.ShopDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 * Created by miou on 2017/6/15 0015.
 */
@RestController
@RequestMapping("/order")
public class OrderDomainController {

    @Autowired
    private OrderAppService orderAppService;

    @Autowired
    private CommandGateway commandGateway;

    /** 检查商品库存
     * @param id 商品ID
     * */
    @RequestMapping("/{id}")
    public void checkProductStock(@PathVariable String id){
        orderAppService.checkProductStock(id);
    }


    /** 生成订单
     *
     * */
    @RequestMapping(value = "/createOrder/{quantity}",method = RequestMethod.POST)
    public void createOrder(@PathVariable Long quantity){
        List<OrderProductDto> products = new ArrayList<>();

        //
        OrderProductDto orderProductDto = new OrderProductDto();
        orderProductDto.setProductId("1");
        orderProductDto.setQuantity(quantity);
        List<CategoryAttribute> attributes = new ArrayList<>();
        CategoryAttribute categoryAttribute = new CategoryAttribute();
        categoryAttribute.setTypeCode("1");
        categoryAttribute.setAttributeCode("1");
        attributes.add(categoryAttribute);
        orderProductDto.setAttributes(attributes);
        products.add(orderProductDto);
        Address address = new Address("1","1","1");
        commandGateway.send(new CreateOrderCommand(new OrderId(),"123456",products,address));

    }


}
